import axios from "axios";
import qs from "querystring";
import {
  Message,
  MessageBox
} from "element-ui";
// 创建axios实例
const service = axios.create({
  timeout: 35000 // 请求超时时间
});
// request拦截器
service.interceptors.request.use(
  config => {
    config.headers["accessToken"] = sessionStorage.getItem("TOKEN") || "";
    config.headers["Content-Type"] = 'application/json';
    return config;
  },
  error => {
    console.log(error); // for debug
    Promise.reject(error);
  }
);
// respone拦截器
service.interceptors.response.use(
  response => {
    /**
     * code为非200是抛错 可结合自己业务进行修改
     */
    const res = response.data;
    if (sessionStorage.getItem("TOKEN")) {
      if (res.statusCode !== 200) {
        if (res.statusCode == 998) {
          MessageBox.alert("你已被登出，点击确定重新登录", "登出", {
            confirmButtonText: "确定",
            showClose: false,
            callback: action => {
              sessionStorage.removeItem('TOKEN')
              location.reload();
            }
          });
          return response.data;
        } else {
          return response.data;
        }
      } else {
        return response.data;
      }
    } else {
      return response.data;
    }

  },
  error => {
    return Promise.reject(error);
  }
);

export {
  service
};
